<template>
  <a-modal title="新建信息" :width="660" :visible="visible" :confirmLoading="loading" @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">

        <a-form-item label="姓名"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', { rules: [{ required: true, min: 5, message: '请输姓名' }] }]"  size="large"/>
        </a-form-item>
        <a-form-item label="证件信息"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['idno', { rules: [{ required: true, min: 18, message: '证件信息' }] }]"  size="large"/>
        </a-form-item>
        <a-form-item label="联系方式"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['phone', { rules: [{ required: true, min: 5, message: '联系方式' }] }]"  size="large"/>
        </a-form-item>
        <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select ref="select" style="width: 120px" size="large">
            <a-select-option v-for="item in sexList" :value="item.id">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="婚姻状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select ref="select" style="width: 120px" size="large">
            <a-select-option v-for="item in hystatusList" :value="item.id">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="紧急联系人"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', { rules: [{ required: true, min: 5, message: '紧急联系人' }] }]"  size="large"/>
        </a-form-item>
        <a-form-item label="紧急联系方式"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', { rules: [{ required: true, min: 5, message: '紧急联系人' }] }]"  size="large"/>
        </a-form-item>
        <a-form-item label="地址"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', { rules: [{ required: true, min: 5, message: '紧急联系人' }] }]"  size="large"/>
        </a-form-item>
        <a-form-item label="备注"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea placeholder="" :rows="4" :maxlength="255"  size="large"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['description', 'pid']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data() {
    this.formLayout = {
      labelCol: {xs: { span: 24 },sm: { span: 7 }},
      wrapperCol: {xs: { span: 24 },sm: { span: 13 }}
    }
    return {
      labelCol: {lg: { span: 5 },sm: { span: 5 }},
      wrapperCol: {xs: { span: 19 },sm: { span: 19 }},
      form: this.$form.createForm(this),
      sexList: [
        { id: 1, name: '男' },
        { id: 2, name: '女' }
      ],
      hystatusList: [
        { id: 1, name: '未婚' },
        { id: 2, name: '已婚' },
        { id: 3, name: '同居' }
      ],
      fileList: [],
      loading: false,
      imageUrl: null,
    }
  },
  created() {
    console.log('custom modal created')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    beforeUpload: file => {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
      if (!isJpgOrPng) {
        message.error('You can only upload JPG file!');
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        message.error('Image must smaller than 2MB!');
      }
      return isJpgOrPng && isLt2M;
    },
    uploadchangfge: info => {
      if (info.file.status === 'uploading') {
        this.loading = true;
        return;
      }
      if (info.file.status === 'done') {
        // Get this url from response in real world.
        getBase64(info.file.originFileObj, base64Url => {
          this.imageUrl = base64Url;
          this.loading = false;
        });
      }
      if (info.file.status === 'error') {
        this.loading = false;
        message.error('upload error');
      }
    }
  }
}
</script>
